package com.yunxin.admin.system.api.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunxin.admin.system.application.dto.*;
import com.yunxin.admin.system.infra.common.constant.SecurityConstants;
import com.yunxin.admin.system.infra.framework.security.SmsAuthenticationToken;
import com.yunxin.admin.system.infra.framework.security.userdetails.SysUserDetails;
import com.yunxin.admin.system.infra.framework.security.util.SecurityUtils;
import com.yunxin.common.exception.BusinessException;
import com.yunxin.common.result.PageResult;
import com.yunxin.common.result.Result;
import com.yunxin.core.admin.command.UserSonAddCommand;
import com.yunxin.core.admin.command.UserSonPageCommand;
import com.yunxin.core.admin.domain.AdminUser;
import com.yunxin.core.admin.domain.AdminUserMerchant;
import com.yunxin.core.admin.service.AdminUserMerchantService;
import com.yunxin.core.admin.service.AdminUserService;
import com.yunxin.core.admin.service.AdminUserSonService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 *
 * @author zero°
 * @since 2023-05-22
 */
@Tag(name = "021.用户子账号接口")
@RestController
@RequestMapping("/api/v1/usersons")
@RequiredArgsConstructor
public class SysUserSonController {
    private final AuthenticationManager authenticationManager;
    @Autowired
    private AdminUserSonService adminUserSonService;
    @Autowired
    private AdminUserMerchantService adminUserMerchantService;

    @Operation(summary = "子用户分页列表", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @GetMapping("/page")
    public Result<Page<AdminUser>> getUserPage(@ParameterObject UserSonPageCommand command) {
        Long userId = SecurityUtils.X.getUser().getUserId();
        command.setUserId(userId.intValue());
        Page<AdminUser> userPage = adminUserSonService.getUserPage(command);
        return Result.success(userPage);
    }

    @Operation(summary = "添加子用户管理", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @PostMapping("/addUseSon")
    public Result addUseSon(@ParameterObject UserSonAddCommand command) {
        command.setUserId(SecurityUtils.X.getUser().getUserId().intValue());
        //验证账号密码是否正确
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
                command.getUsername().toLowerCase().trim(),
                command.getPassword()
        );
        Authentication authentication = authenticationManager.authenticate(authenticationToken);
        SysUserDetails userDetails = (SysUserDetails) authentication.getPrincipal();
        if (userDetails.getUserId().intValue()==SecurityUtils.X.getUser().getUserId().intValue()){
            throw new BusinessException("不能填写该账号的账号密码");
        }
        if (ObjectUtils.isEmpty(userDetails)){
            throw new BusinessException("账号密码错误");
        }
        command.setToUserId(userDetails.getUserId().intValue());
        adminUserSonService.addUseSon(command);
        return Result.success();
    }

    @Operation(summary = "删除子用户管理", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @DeleteMapping("/deleteUseSonById")
    public Result deleteUseSonById(@Parameter(description = "用户id") @RequestParam Integer id) {
        int userId = SecurityUtils.X.getUser().getUserId().intValue();
        adminUserSonService.deleteUseSonById(userId,id);
        return Result.success();
    }

}
